/**
 * @author kzm
 * @version 1.0
 *
 * @license MIT-License (2007)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 
 */
package com.mediacatalyst.net {
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	
	
	import com.mediacatalyst.lang.ICompareable;
	
	import com.mediacatalyst.lang.IDeconstructable;
	
	import com.mediacatalyst.lang.INaturalObject;
	
	import com.mediacatalyst.lang.deconstructObject;
	
	import com.mediacatalyst.patterns.prototype.IPrototype;
	
	import com.mediacatalyst.utils.ILinkedNode;	
	
	public class LinkedLoader extends URLLoader implements ILinkedNode,INaturalObject,IDeconstructable{
		private var _url:URLRequest;
		private var _next:ILinkedNode;

		public function LinkedLoader(url:URLRequest){
			super();
			_url = url;
		}
		/**
		 * @see com.mediacatalyst.lang.IDeconstructable#deconstruct()
		 */
		public function deconstruct():void{
			deconstructObject(_url);
			deconstructObject(_next);
		}

		public function get url():String{
			return _url.url;
		}
		/**
		 * @see com.mediacatalyst.utils.ILinkedNode#next
		 */	
		public function set next(node:ILinkedNode):void{
			if(node!=null)
				_next = LinkedLoader(node);
			else
				_next = null;
		}
		/**
		 * @see com.mediacatalyst.lang.Iteratable#hasNext()
		 */	
		public function hasNext():Boolean{
			return _next!=null;
		}
		/**
		 * @see com.mediacatalyst.lang.Iteratable#next
		 */	
		public function get next():*{
			if(!hasNext())
				return null;
			return ILinkedNode(_next);
		}
		/**
		 * @see flash.net.URLLoader#load()
		 */
		override public function load(url:URLRequest):void{
			_url = url;
			super.load(_url);
		}
		/**
		 * Load url associated with this object.
		 */
		public function loadURL():void{
			super.load(_url);
		}
		/**
		 * @see com.mediacatalyst.lang.IPrintable#toString()
		 */
		override public function toString():String{
			return "LinkedLoader(url="+url+",next="+(hasNext()?LinkedLoader(next).toString():"null")+")";
		}
		/**
		 * @see com.mediacatalyst.lang.ICompareable#equals()
		 */
		public function equals(other:ICompareable):Boolean{
			if(!(other is LinkedLoader))
				return false;
			var n:Boolean = false;
			if(hasNext()&&LinkedLoader(other).hasNext()){
				n = LinkedLoader(next).equals(LinkedLoader(other).next);
			}
			return LinkedLoader(other).url==url && n;
		}
		/**
		 * @see com.mediacatalyst.patterns.prototype.IPrototype#clone()
		 */
		public function clone():IPrototype{
			var ll:LinkedLoader = new LinkedLoader(new URLRequest(url));
			ll.next = LinkedLoader(INaturalObject(next).clone());
			return ll;
		}
	}
}